#include <iostream>
using namespace std;

class String
{
public:
 String(char* str = "")
 :_str(new char[strlen(str)]+1)
 , _refCount(new int(1))
 {
 strcpy(_str, str);
 }

 String(const String& str)
 : _str(str._str)
 ,_refCount(str._refCount)
 {
 (*_refCount)++;
 }

 ~String()
 {
 release();
 }

 String& operator= (const String& s)
 {
 if (_str != s._str)
 {
  release();
  _refCount = s._refCount;
  (*_refCount)++;
  _str = s._str;
 }
 return *this;
 }

 void release()
 {
 if ((*--_refCount) == 0)
 {
  delete[] _str;
  delete _refCount;
 }
 }

private:
 char* _str;
 int* _refCount;
};